package love.yuanmengrobot.als.system.servlet;

import love.yuanmengrobot.als.system.dao.AccountManagementDao;
import love.yuanmengrobot.als.system.dao.impl.AccountManagementDaoImpl;
import love.yuanmengrobot.als.system.javaBean.AlsUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

@WebServlet(name = "AccountManagementServlet")
public class AccountManagementServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        AccountManagementDao amd = new AccountManagementDaoImpl();
        String op =request.getParameter("op");
        switch (op){
            case "0":
                if (amd.addUser(getNewUser(request))){
                    request.setAttribute("add", true);
                }else request.setAttribute("add", false);
                break;
            case "1":
                if( amd.updatePassword(request.getParameter("update_Id"),request.getParameter("newPassword"))){
                    request.setAttribute("update", true);
                }else request.setAttribute("update", false);
                break;
            case "2":
                if (amd.deleteUser(request.getParameter("deletetId"))){
                    request.setAttribute("delete", true);
                }else {
                    request.setAttribute("delete", false);
                }
                break;
            case "3":
                search(request.getParameter("searchContent"),request,response);
                break;
            default:break;

        }

        toView(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        toView(request, response);
    }

    public void toView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountManagementDao amd = new AccountManagementDaoImpl();
        List<AlsUser> allUser = amd.allUsers();
        request.setAttribute("allUser",allUser);
        request.getRequestDispatcher("WEB-INF/view/system/accountManagement/accountManagement.jsp").forward(request, response);
    }


    public AlsUser getNewUser(HttpServletRequest request)  {
        AlsUser newUser = new AlsUser();
        newUser.setUsername(request.getParameter("newusername"));
        newUser.setUserpassword(request.getParameter("newpassword"));
        newUser.setUserphone(request.getParameter("newphone"));
        newUser.setUseremail(request.getParameter("newemail"));
        return newUser;
    }

    public void search(String content,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountManagementDao amd = new AccountManagementDaoImpl();
        List<AlsUser> allUser = amd.allUsers();
        Iterator<AlsUser> iterator = allUser.iterator();
        while(iterator.hasNext()){
            if(!iterator.next().getUsername().contains(content)) {
                iterator.remove();
            }
        }
        request.setAttribute("historySearch",content);
        request.setAttribute("allUser",allUser);
        request.getRequestDispatcher("WEB-INF/view/system/accountManagement/accountManagement.jsp").forward(request, response);
    }

    private static boolean isnumber(String num) {
        if (num == null || "".equals(num)) return false;
        Pattern pt = Pattern.compile("^[-+]?[\\d]*$");
        return pt.matcher(num).matches();
    }

}
